<template>
  <Dialog
    v-model="dialogVisible"
    :title="t('reviewJudgeScene.resultConfirm.dialog.suggestList')"
    width="80%"
  >
    <ContentWrap>
      <el-button
        :disabled="loading || !list.length || isFinalResultConfirmed"
        type="success"
        @click="handleConfirmResult"
        >{{ t('reviewJudgeScene.resultConfirm.dialog.confirmResult') }}
      </el-button>
    </ContentWrap>
    <vxe-table
      id="iInitialJudgeResultForm-table"
      v-loading="loading"
      :data="list"
      stripe
      :custom-config="vxeTableConfig"
      :column-config="{ resizable: true }"
      :size="vxeTableSize"
    >
      <vxe-column :title="t('common.index')" type="seq" field="seq" width="60" align="center" />
      <vxe-column
        :title="t('candidate.fields.jobCode')"
        field="jobCode"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('candidate.fields.studentName')"
        field="studentName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('candidate.fields.major')"
        field="major"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('reviewJudgeScene.resultConfirm.dialog.awardLevel')"
        field="awardLevel"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('reviewJudgeScene.resultConfirm.dialog.initialScore')"
        field="initialScore"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('reviewJudgeScene.resultConfirm.dialog.finalScore')"
        field="finalScore"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        v-if="awardsInfo.awardsType === 0 && currentRound?.reviewMethod === 'SORT'"
        :title="t('judgeScene.initial.sort')"
        field="sort"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        v-if="awardsInfo.awardsType === 0 && currentRound?.reviewMethod === 'SORT'"
        :title="t('judgeScene.initial.sameAmount')"
        field="sameAmount"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        v-if="awardsInfo.awardsType === 0"
        :title="t('judgeScene.initial.isWin')"
        field="isWin"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      >
        <template #default="scope">
          <span v-if="scope.row.isWin">{{ t('common.yes') }}</span>
          <span v-else>{{ t('common.no') }}</span>
        </template>
      </vxe-column>
      <vxe-column
        v-if="awardsInfo.awardsType === 0"
        :title="t('judgeScene.initial.differenceSelection')"
        field="differenceSelection"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      >
        <template #default="scope">
          <span v-if="scope.row.differenceSelection">{{ t('common.yes') }}</span>
          <span v-else>{{ t('common.no') }}</span>
        </template>
      </vxe-column>
      <vxe-column
        v-if="awardsInfo.awardsType === 1"
        :title="t('reviewJudgeScene.resultConfirm.dialog.suggestLevel')"
        field="value"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
    </vxe-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </Dialog>
</template>

<script setup lang="ts">
import { ReviewSceneApi, StudentAwardResultResVO } from '@/api/awards/reviewscene'
import type { VxeTablePropTypes } from 'vxe-table'
import { useAppStore } from '@/store/modules/app'
import { ScxFinalWinningRecordApi } from '@/api/awards/scxfinalwinningrecord'
import { useInstanceReviewSceneStore } from '@/store/modules/instance/instanceReivewScene'
import { storeToRefs } from 'pinia'
import { FinalWinApi } from '@/api/awards/finalwin'

defineOptions({ name: 'ReviewJudgeResultForm' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const instanceReviewSceneStore = useInstanceReviewSceneStore()
const { awardsId, awardsInfo, currentRound, isFinalResultConfirmed } =
  storeToRefs(instanceReviewSceneStore)

const dialogVisible = ref(false) // 弹窗的是否展示
const loading = ref(true) // 列表的加载中
const list = ref<StudentAwardResultResVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数

const queryParams = reactive<any>({
  pageNo: 1,
  pageSize: 10,
  awardsId: undefined,
  awardsInstanceId: undefined
})

const vxeTableConfig: VxeTablePropTypes.CustomConfig = {
  storage: true,
  mode: 'drawer',
  checkMethod({ column }) {
    return !['seq', 'operator'].includes(column.field)
  }
}

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

const getList = async () => {
  loading.value = true
  try {
    let data: any
    if (awardsInfo.value.awardsType === 0) {
      queryParams.awardsId = awardsId.value
      data = await FinalWinApi.getFinalWinPageVo(unref(queryParams))
    }
    if (awardsInfo.value.awardsType === 1) {
      queryParams.awardsInstanceId = awardsId.value
      data = await ScxFinalWinningRecordApi.getScxFinalWinningRecordPage(unref(queryParams))
    }
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const open = () => {
  resetForm()
  dialogVisible.value = true
  getList()
}

const resetForm = () => {
  list.value = []
  total.value = 0
  queryParams.pageNo = 1
  queryParams.pageSize = 10
  queryParams.awardsInstanceId = undefined
  queryParams.awardsId = undefined
}

// 确认结果
const confirmLoading = ref(false)
const handleConfirmResult = async () => {
  if (!awardsId.value || !currentRound.value?.roundId) {
    return
  }
  try {
    await message.confirm(t('reviewJudgeScene.resultConfirm.dialog.confirmResultTips'))
  } catch {
    return
  }
  confirmLoading.value = true
  try {
    await ReviewSceneApi.judgesConfirmStudentAwardResult(awardsId.value, currentRound.value.roundId)
    instanceReviewSceneStore.getFinalResultConfirmed().then()
    message.success(t('reviewJudgeScene.resultConfirm.dialog.confirmSuccess'))
    dialogVisible.value = false
  } finally {
    confirmLoading.value = false
  }
}

defineExpose({ open }) // 提供 open 方法，用于打开弹窗
</script>
